<?php
class Content {
	 public function spin_content($content) {
        $zz = substr_count($content, "}");
        $m  = 0;
        while ($m < $zz) {
            $nn     = strpos($content, "}");
            $y      = substr($content, 0, $nn);
            $p      = strrpos($y, "{");
            $bdata  = substr($content, $p, $nn - $p + 1);
            $bdata2 = eregi_replace("{|}", "", $bdata);
            $cspin  = explode("|", $bdata2);
            shuffle($cspin);
            $newspin = $cspin[0];
            $content = str_replace($bdata, $newspin, $content);
            ++$m;
        }
        return $content;
    }

	public function getXML($xml , $value , $keyword) {
        /* TAG
        {asin} , {title} , {keyword} , {url},{features} , 
        {rating} , {listprice} , {price}, {thumbnail} ,
        {smallimage}, {mediumimage}, {largeimage}
         *
        */
        $data = "";

		$asin = $xml[ASIN];
		$title = $xml[ItemAttributes][Title];
		$iFeature = $xml[ItemAttributes][Feature];
		$iContent = $xml[EditorialReviews];
		$url = $xml[DetailPageURL];
		$sImage = $xml[SmallImage][URL];
		$mImage = $xml[MediumImage][URL];
		$lImage = $xml[LargeImage][URL];
		$thumb = $xml[SmallImage][URL];
		$listPrice = $xml[ItemAttributes][ListPrice][FormattedPrice];
		$price = $xml[OfferSummary][LowestNewPrice][FormattedPrice];

		// REVIEWS -  Replace for Nov 11 API Change
        //$product_review = $item->CustomerReviews->IFrameURL;

		$product_review = $xml[CustomerReviews][IFrameURL];

        $reviewsiframe = '<iframe style="margin-top: 10px;" src="'.$product_review.'" width="580px" height="500px" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0"><p>Your browser does not support iframes.</p></iframe>';

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$product_review);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
        $revcontent = curl_exec($ch);
        curl_close($ch);

        $revcontent = ereg_replace('[[:space:]]+', ' ', trim($revcontent));

        if (preg_match('~<body[^>]*>(.*?)</body>~si', $revcontent, $body)) {
            $reviewsnoiframe = str_replace('class="crVotingButtons">', "", $body[1]);
        } else {
            $reviewsnoiframe = "";
        }

        if($reviewsnoiframe != "") {
            $reviewsnoiframe = str_replace('class="crIFrame"', ' style="margin:0px;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameLogo"', ' style="margin-bottom:20px;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameHeaderLeftColumn"', ' style="float:left;margin-right:20px;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIframeHeaderTitle"', ' style="font-size:24px;letter-spacing:-1px;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameAverageReviewText"', ' style="font-weight:bold;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameNumCustReviews"', ' style="white-space:nowrap;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameHeaderHistogram"', ' style="float:left;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="tiny"', ' style=font-family:verdana,arial,helvetica,sans-serif;font-size:x-small;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIframeReviewList"', ' style="clear:left;width:100%;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('<a ', ' <a rel="nofollow" ', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('<div style="padding-top: 10px; clear: both; width: 100%;"', ' <div style="padding-top: 10px; clear: both; width: 100%;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameCreateReview"', ' style="display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="tiny verifyWhatsThis"', ' style="display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('href="/gp/help/customer/display.html/', ' href="http://www.amazon.'.$site.'/gp/help/customer/display.html/', $reviewsnoiframe);

            if ($keywords == "") {
                $reviewsnoiframe = '<h3>'.$title.' Reviews</h3></br>'.$reviewsnoiframe;
            }else {
                $reviewsnoiframe = '<h3>'.$keyword.' : '.$title.' Reviews</h3></br>'.$reviewsnoiframe;
            }

        }

		// Customer Rating
        $customer_rating_url = $url;

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$customer_rating_url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
        $customer_rating_data  = curl_exec($ch);
        curl_close($ch);

        $customer_rating_data = explode("out of 5 stars", $customer_rating_data);
        $customer_rating = str_replace('" >','',$customer_rating_data[1]);
        $customer_rating = str_replace('<span>','',$customer_rating);

        $rating = Content::wpr_star_rating($customer_rating);

        $content = "";

		if(count($iContent) > 0) {
			foreach (@$iContent as $val) {
				$content .='<h2>'.$val[Source].'</h2>';
				$content .='<p>'.$val[Content].'</p>';
			}
		}

		if(count($iFeature) > 0) {
        $feature = "<ul>";
        foreach (@$iFeature as $val) {
            $feature .="<li>".$val."</li>";
        }
        $feature .="</ul>";
		}

		$data = str_replace('{asin}', $asin, $data);
		$data = str_replace('{title}', $title, $value);
        $data = str_replace('{keyword}', $keyword, $data);
        $data = str_replace('{content}', $content, $data);
        $data = str_replace('{features}', $feature, $data);
        $data = str_replace('{url}', $url, $data);
        $data = str_replace('{listprice}', $listPrice, $data);
        $data = str_replace('{price}', $price, $data);
        $data = str_replace('{smallimage}', $sImage, $data);
        $data = str_replace('{mediumimage}', $mImage, $data);
        $data = str_replace('{largeimage}', $lImage, $data);
        $data = str_replace('{thumbnail}', $thumb, $data);
        $data = str_replace('{rating}', $rating, $data);
        $data = str_replace('{reviews}', $reviewsnoiframe, $data);

        return $data;
		
    }

	public function wpr_star_rating($rating) {

        $imagepath = get_option('siteurl').'/wp-content/plugins/cbrobot/images/';	
		//$imagepath = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__)); 
		//$imagepath = str_replace("/modules/","/images/",$imagepath);

		if($rating>=0 && $rating <= 0.7) {
			$image = '<img src="'.$imagepath.'rating_0.5.gif" class="starrating" />';
		}
		if($rating>=1.3 && $rating <= 1.7) {
			$image = '<img src="'.$imagepath.'rating_1.5.gif" class="starrating" />';
		}
		if($rating>=2.3 && $rating <= 2.7) {
			$image = '<img src="'.$imagepath.'rating_2.5.gif" class="starrating" />';
		}
		if($rating>=3.3 && $rating <= 3.7) {
			$image = '<img src="'.$imagepath.'rating_3.5.gif" class="starrating" />';
		}
		if($rating>=4.3 && $rating <= 4.7) {
			$image = '<img src="'.$imagepath.'rating_4.5.gif" class="starrating" />';
		}
		if($rating>=0.8 && $rating <= 1.2) {
			$image = '<img src="'.$imagepath.'rating_1.0.gif" class="starrating" />';
		}
		if($rating>=1.8 && $rating <= 2.2) {
			$image = '<img src="'.$imagepath.'rating_2.0.gif" class="starrating" />';
		}
		if($rating>=2.8 && $rating <= 3.2) {
			$image = '<img src="'.$imagepath.'rating_3.0.gif" class="starrating" />';
		}
		if($rating>=3.8 && $rating <= 4.2) {
			$image = '<img src="'.$imagepath.'rating_4.0.gif" class="starrating" />';
		}
		if($rating>=4.8 && $rating <= 5) {
			$image = '<img src="'.$imagepath.'rating_5.0.gif" class="starrating" />';
		}
		 
		return $image;
    }

}